﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
//
using hrModel;
using DataAccess.Data;

namespace hrBll
{
    public class BLLLogin
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static int Add(Login model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Login(");
            strSql.Append("EmpNO,LoginName,LoginCode,Password,RoleID,IsUsed,CreateName,CreateDatetime,Remark");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("" + model.EmpNO + ",");
            strSql.Append("'" + model.LoginName + "',");
            strSql.Append("'" + model.LoginrCode + "',");
            strSql.Append("'" + model.Password + "',");
            strSql.Append("" + model.RoleID + ",");
            strSql.Append("'" + model.IsUsed + "',");
            strSql.Append("'" + model.CreateName + "',");
            strSql.Append("'" + model.CreateDatetime + "',");
            strSql.Append("'" + model.Remark + "'");
            strSql.Append(")");
            return DbHelperSQL.Instance.ExcuteNonQuery(strSql.ToString());
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static int Update(Login model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Login set ");
           strSql.Append("LoginName='" + model.LoginName + "',");
            strSql.Append("Password='" + model.Password + "',");
            strSql.Append("RoleID=" + model.RoleID + ",");
            strSql.Append("IsUsed='" + model.IsUsed + "',");
            strSql.Append("CreateName='" + model.CreateName + "',");
            strSql.Append("CreateDatetime='" + model.CreateDatetime + "',");
            strSql.Append("Remark='" + model.Remark + "'");
            strSql.Append(" where LoginCode ='"+ model.LoginrCode+"'");
            return DbHelperSQL.Instance.ExcuteNonQuery(strSql.ToString());
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static int Delete(string LoginCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete Login ");
            strSql.Append(" where LoginID='" + LoginCode + "'");
            return DbHelperSQL.Instance.ExcuteNonQuery(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Login GetLogin(string LoginrCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select   EmpID, LoginName, LoginCode, Password, RoleID, IsUsed, CreateName,CreateDatetime,Remark FROM Login ");
            strSql.Append(" where LoginrCode=" + LoginrCode);
            Login model = new Login();
            DataTable dt = DbHelperSQL.Instance.GetDataTable(strSql.ToString());

            if (dt.Rows.Count > 0)
            {

                model.EmpNO = dt.Rows[0]["EmpNO"].ToString();
                model.LoginName = dt.Rows[0]["LoginName"].ToString();
                model.LoginrCode = dt.Rows[0]["LoginCode"].ToString();
                model.Password = dt.Rows[0]["Password"].ToString();
                if (dt.Rows[0]["RoleID"].ToString() != "")
                {
                    model.RoleID = int.Parse(dt.Rows[0]["RoleID"].ToString());
                }
                model.IsUsed = dt.Rows[0]["IsUsed"].ToString();
                model.CreateName = dt.Rows[0]["CreateName"].ToString();
                if (dt.Rows[0]["CreateDatetime"].ToString() != "")
                {
                    model.CreateDatetime = DateTime.Parse(dt.Rows[0]["CreateDatetime"].ToString());
                }
                model.Remark = dt.Rows[0]["Remark"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 检查该员工是否应该是系统用户
        /// </summary>
        /// <param name="EmpID">员工编号</param>
        /// <returns>检查该员工是否应该是系统用户""或EMPID </returns>
        public static string CheckEmp(string LoginrCode)
        {
            return DbHelperSQL.Instance.ExecuteScalar("SELECT LoginID FROM Login WHERE  LoginCode= '" + LoginrCode + "'");
        }

        /// <summary>
        /// 检查用户编码是否可以使用
        /// </summary>
        /// <param name="LoginrCode">用户编码</param>
        /// <returns>用户编码或""</returns>
        public static string CheckLoginCode(string LoginrCode)
        {
            return DbHelperSQL.Instance.ExecuteScalar("SELECT LoginrCode FROM Login WHERE LoginCode='" + LoginrCode + "'");
        }

        /// <summary>
        /// 通过LoginID查找相应的密码
        /// </summary>
        /// <param name="LoginID"></param>
        /// <returns></returns>
        public static string GetPwdByLoginID(string LoginID)
        {
            return DbHelperSQL.Instance.ExecuteScalar("Select Password from Login Where LoginID = " + LoginID);
        }
    }
}
